package com.wxl.common.adapter

import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.R
import com.wxl.common.bean.TagItem

/**
 * create file time : 2021/4/12
 * create user : wxl
 * subscribe :
 */
class TagAdapetr : BaseQuickAdapter<TagItem,BaseViewHolder>{

    private val selectIndexs = arrayListOf(0)

    constructor(data: MutableList<TagItem>?) : super(R.layout.tag_select_item,data)


    override fun convert(helper: BaseViewHolder, item: TagItem?) {
        item?.run {
            helper.setText(R.id.tagItemText,text)
            helper.itemView.setBackgroundResource(if(isSelect)R.drawable.shape_red_light_c10 else R.drawable.shape_gray_light_c10)
            helper.setTextColor(R.id.tagItemText, ContextCompat.getColor(mContext,if(isSelect) android.R.color.holo_red_light else R.color.text_black_color))
        }
    }

    fun onSelectSingle(position:Int){
        onResetSelectData()
        selectIndexs.clear()
        selectIndexs.add(position)
        data[position].isSelect = true
        notifyItemChanged(position)
    }

    private fun onResetSelectData(){
        selectIndexs.forEach {
            data[it].isSelect = false
        }
        notifyDataSetChanged()
    }

    fun onSelectMutil(position: Int){
        if(position == 0){
            onSelectSingle(position)
        } else {
            if(selectIndexs[0] == 0){
                selectIndexs.clear()
            }
            data[0].isSelect = false
            data[position].isSelect = true
            selectIndexs.add(position)
            notifyDataSetChanged()
        }
    }

    fun getSelectPosition():ArrayList<Int>{
        return selectIndexs
    }

}